package com.example.config;

import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class ConfrimMsg implements RabbitTemplate.ReturnsCallback,RabbitTemplate.ConfirmCallback {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(this::confirm);
        rabbitTemplate.setReturnsCallback(this::returnedMessage);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        if(b){

        }else {
            System.out.println(s);
            System.out.println(correlationData.getId());
        }
    }

    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        System.out.println(returnedMessage.getMessage());
        System.out.println(returnedMessage.getExchange());
        System.out.println(returnedMessage.getReplyCode());
        System.out.println(returnedMessage.getReplyText());
        System.out.println(returnedMessage.getRoutingKey());
    }
}
